package com.order.manager.controller.Img;




import com.order.manager.Model.UploadImgInfo;
import com.order.manager.Model.other.RestBean;
import com.order.manager.service.impl.UploadImgInfoServiceImpl;
import com.order.manager.util.ImageUtil;
import com.order.manager.util.JedisUtil;
import com.order.manager.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/api/upload")
@Slf4j
public class UploadController {

    @Resource
    UploadImgInfoServiceImpl uploadImgInfoService;

    @Resource
    JwtUtil jwtUtil;

    @Resource
    JedisUtil jedisUtil;


    @Value("${img.position}")
    String imgPosition;

    @Value("${location.port}")
    String serverAddress;

    @PostMapping("/img")
    public RestBean upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String jwtUUID = jwtUtil.getJwtUsername(request);
        log.info(jwtUUID);
        UploadImgInfo uploadImgInfo = new UploadImgInfo();
        try {
            if (file.isEmpty()) {
                return RestBean.failure(400, "上传的文件为空");
            }
            String originalFileName = file.getOriginalFilename();
            uploadImgInfo.setImgName(originalFileName);
            if (originalFileName == null || originalFileName.isEmpty()) {
                return RestBean.failure(400, "文件名无效");
            }
            String fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString();
            String newFileName = uuid + fileExtension;
            uploadImgInfo.setImgSize((double) file.getSize());
            uploadImgInfo.setInputTime(new Date());
            uploadImgInfo.setUserId(jwtUUID);
            String filePath = imgPosition + newFileName;
            File directory = new File(imgPosition);
            if (!directory.exists() && (!directory.mkdirs())) {
                return RestBean.failure(500, "文件夹初始化失败");
            }
            file.transferTo(new File(filePath));
            log.info("文件保存成功，路径：" + filePath);//该图像的存储地址
            String url = "http://" + serverAddress + "/img/" + newFileName;
            log.info("http://" + serverAddress + "/img/" + newFileName);//该图像的访问地址
            uploadImgInfo.setImgHref(url);
            uploadImgInfoService.save(uploadImgInfo);//保存上传信息
            return RestBean.success(url);
        } catch (IOException e) {
            log.error("文件保存失败：" + e.getMessage());
            return RestBean.failure(500, "文件保存失败");
        }
    }


    @PostMapping("/img-d")
    public RestBean Dupload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String jwtUUID = jwtUtil.getJwtUsername(request);
        log.info(jwtUUID);
        UploadImgInfo uploadImgInfo = new UploadImgInfo();
        try {
            if (file.isEmpty()) {
                return RestBean.failure(400, "上传的文件为空");
            }
            String originalFileName = file.getOriginalFilename();
            uploadImgInfo.setImgName(originalFileName);
            if (originalFileName == null || originalFileName.isEmpty()) {
                return RestBean.failure(400, "文件名无效");
            }
            String fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString();
            String originImg=imgPosition+"origin:"+uuid+fileExtension;
            String newFileName = uuid + fileExtension;
            uploadImgInfo.setImgSize((double) file.getSize());
            uploadImgInfo.setInputTime(new Date());
            uploadImgInfo.setUserId(jwtUUID);
            String filePath = imgPosition + newFileName;
            File directory = new File(imgPosition);
            if (!directory.exists() && (!directory.mkdirs())) {
                return RestBean.failure(500, "文件夹初始化失败");
            }
            file.transferTo(new File(originImg));
            ImageUtil.imgScale(originImg,filePath,0.2);
            log.info("文件保存成功，路径：" + filePath);//该图像的存储地址
            String url = "http://" + serverAddress + "/img/" + newFileName;
            log.info("http://" + serverAddress + "/img/" + newFileName);//该图像的访问地址
            uploadImgInfo.setImgHref(url);
            uploadImgInfoService.save(uploadImgInfo);//保存上传信息
            return RestBean.success(url);
        } catch (IOException e) {
            log.error("文件保存失败：" + e.getMessage());
            return RestBean.failure(500, "文件保存失败");
        }
    }


}
